<?php

class Ads_Form_Searchsettings extends Zend_Form {

    private $_translate = null;

    public function init() {
        $this->_translate = Zend_Registry::get('Zend_Translate');
    }

    public function main($details) {
        $settingDb = new Ads_Model_Rentsetting();
        $this->setMethod('post');

//        #city
        $cityDb = new Ads_Model_City();
        $cityArray = $cityDb->fetchAll();
//        $city = new Zend_Form_Element_Multiselect('city');
//        $city->setLabel('Select City(s):')
//                ->setAttribs(array('class' => 'span12'))
//                ->setOptions(array('title' => 'Select City(s)'));
//        foreach ($cityArray as $value) {
//            $city->addMultiOption($value->id, $value->name);
//        }
        #state
        $stateDb = new Ads_Model_State();
        $stateArray = $stateDb->fetchAll();
        $state = new Zend_Form_Element_Multiselect('state');
        $state->setLabel($this->_translate->_('select_area_location') . ':')
                ->setAttribs(array('class' => 'span12'))
                ->setOptions(array('title' => $this->_translate->_('select_area_location')));
        foreach ($stateArray as $value) {
            $state->addMultiOption($value->id, $value->name);
        }

        #zipcode
        $zipDb = new Ads_Model_Zipcode();
        $zipArray = $zipDb->fetchAll();
        $zipcode = new Zend_Form_Element_MultiSelect('zipcode');
        $zipcode->setLabel($this->_translate->_('select_zip') . ':')
                ->setAttribs(array('class' => 'span12'))
                ->setOptions(array('title' => $this->_translate->_('select_zip')));
//        foreach ($zipArray as $value) {
//            $zipcode->addMultiOption($value->id, $value->codes);
//        }
        foreach ($zipArray as $value) {
            $zipId = $value->id;
            $zipCode = $value->codes;
            $cityId = $value->city_id;
            $cityName = $cityArray[$cityId - 1]['name'];
            $zipcode->addMultiOption($zipId, $zipCode . ' ' . $cityName);
        }

        #property type
        $propertyType = new Zend_Form_Element_Multiselect('propertyType');
        $proArray = $settingDb->getProprertyTypeArray()->property_type;
        $pkeys = array_keys($proArray);
        $propertyType->setLabel($this->_translate->_('select_property_type') . ':')
                ->setAttribs(array('class' => 'span12'))
                ->setAttribs(array('title' => $this->_translate->_('select_property_type')))
                ->addValidator('Int')
                ->addFilter('HtmlEntities');
        for ($index = 0; $index < count($proArray); $index++) {
            $propertyType->addMultiOption($pkeys[$index], $proArray[$index]);
        }

        #rental period
        $rentalPeriod = new Zend_Form_Element_Multiselect('rentalPeriod');
        $rparray = $settingDb->getRentalPeriodArray()->rental_period;
        $rpkeys = array_keys($rparray);
        $rentalPeriod->setLabel($this->_translate->_('select_rental_period') . ':')
                ->setAttribs(array('class' => 'span12'))
                ->addValidator('Int')
                ->setAttribs(array('title' => $this->_translate->_('select_rental_period')))
                ->addFilter('HtmlEntities');
        for ($index1 = 0; $index1 < count($rparray); $index1++) {
            $rentalPeriod->addMultiOption($rpkeys[$index1], $rparray[$index1]);
        }

        #furnish type
        $furnishType = new Zend_Form_Element_Multiselect('furnishType');
        $furarray = $settingDb->getFurnishTypeArray()->furnish_type;
        $furkey = array_keys($furarray);
        $furnishType->setLabel($this->_translate->_('select_furnish_type') . ':')
                ->setAttribs(array('class' => 'span12'))
                ->setOptions(array('title' => $this->_translate->_('select_furnish_type')))
                ->addValidator('Int')
                ->addFilter('HtmlEntities');
        for ($index3 = 0; $index3 < count($furarray); $index3++) {
            $furnishType->addMultiOption($furkey[$index3], $furarray[$index3]);
        }

        #rooms
        $rooms = new Zend_Form_Element_Select('rooms');
        $rooms->setLabel($this->_translate->_('select_rooms') . ':')
                ->setAttribs(array('class' => 'span12'))
                ->setOptions(array('title' => $this->_translate->_('select_rooms')))
                ->addMultiOptions(array(
                    0 => $this->_translate->_('select_rooms'),
                    1 => '1',
                    2 => '2',
                    3 => '3',
                    4 => '4',
                    5 => '5',
                    6 => '6',
                    7 => '7',
                    8 => '8',
                    9 => '9',
                    10 => '10+',
        ));

        #area
        $area = new Zend_Form_Element_Select('area');
        $area->setLabel($this->_translate->_('select_size') . ':')
                ->setOptions(array('title' => $this->_translate->_('select_size')))
                ->setAttribs(array('class' => 'span12'))
                ->addMultiOptions(array(
                    0 => $this->_translate->_('select_size'),
                    1 => '1-50',
                    2 => '51-100',
                    3 => '101-150',
                    4 => '151-200',
                    5 => '201-250',
                    6 => '251+',
        ));

        #rent
        $rent = new Zend_Form_Element_Select('rent');
        $rent->setLabel($this->_translate->_('select_rent') . ':')
                ->setOptions(array('title' => $this->_translate->_('select_rent')))
                ->setAttribs(array('class' => 'span12'))
                ->addMultiOptions(array(
                    0 => $this->_translate->_('select_rent'),
                    1 => '1-500',
                    2 => '501-1000',
                    3 => '1001-1500',
                    4 => '1501-2000',
                    5 => '2001-2500',
                    6 => '2501+',
        ));


        if ($details != null) {
            #city
//            $citySettings = $details->city;
//            $city->setValue($citySettings);
            #state
            $stateSettings = $details->state;
            $state->setValue($stateSettings);
            #zipcode
            $zipSettings = $details->zipcode;
            $zipcode->setValue($zipSettings);
            #property type
            $propertyTypeSettings = $details->propertyType;
            $propertyType->setValue($propertyTypeSettings);
            #rental period
            $rentalPeriodSettings = $details->rentalPeriod;
            $rentalPeriod->setValue($rentalPeriodSettings);
            #furnish type
            $furnishTypeSettings = $details->furnishType;
            $furnishType->setValue($furnishTypeSettings);
            #rooms
            $roomSettings = $details->rooms;
            $rooms->setValue($roomSettings);
            #area
            $areaSettings = $details->area;
            $area->setValue($areaSettings);
            #rent
            $rentSettings = $details->rent;
            $rent->setValue($rentSettings);
        }



        #submit
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($this->_translate->_('save'))
                ->setAttribs(array('class' => 'btn btn-success'));

        $this->addElements(array($state, $zipcode, $propertyType, $rentalPeriod, $furnishType, $rooms, $area, $rent, $submit));
    }

}

